package com.gsafety.wellsfort.service.knowledge;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.dao.knowledge.AnswerOptionDAO;
import com.gsafety.wellsfort.dao.knowledge.QuestionDAO;
import com.gsafety.wellsfort.dao.security.AdminDAO;
import com.gsafety.wellsfort.domain.dto.question.QuestionDTO;
import com.gsafety.wellsfort.domain.dto.question.QuestionQueryDTO;
import com.gsafety.wellsfort.domain.model.knowledge.AnswerOption;
import com.gsafety.wellsfort.domain.model.knowledge.Question;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.vo.QuestionExcelVO;
import com.gsafety.wellsfort.domain.vo.QuestionVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class QuestionService {
    @Autowired(required = false)
    QuestionDAO questionDAO;
    @Autowired(required = false)
    AnswerOptionDAO answerOptionDAO;
    @Autowired(required = false)
    AdminDAO adminDAO;

    public ApiResponse<QuestionDTO> saveQuestion(QuestionDTO questionDto) {
        ApiResponse apiResponse = null;
        Question q = new QuestionDTO.POBuilder(questionDto).build();
        List<AnswerOption> answerOptionList = questionDto.getAnswerOptionList();
        Admin admin=LoginContextUtil.getAdmin();
        q.setUpdateTime(new Date());
        if(admin.getId()!=null)q.setOperator(admin.getId());
        if (questionDto.getId() != null && questionDto.getId() != 0) {
            q.setUpdateTime(new Date());
            if(admin!=null)q.setOperator(admin.getId());
            questionDAO.updateByPrimaryKeySelective(q);
            //先清除后增加
            answerOptionDAO.deleteByQuestionId(questionDto.getId());
            for (AnswerOption answerOpt : answerOptionList) {
                AnswerOption answerOptDB = new AnswerOption();
                answerOptDB.setQuestionId(questionDto.getId());
                answerOptDB.setIsRight(answerOpt.getIsRight());
                answerOptDB.setOptionText(answerOpt.getOptionText());
                answerOptionDAO.insertSelective(answerOptDB);
                //有可能修改新增选项
                /*if(answerOpt.getId()==null){
                }else{

                    AnswerOption answerOptDB = answerOptionDAO.selectByPrimaryKey(answerOpt.getId());
                    answerOptDB.setIsRight(answerOpt.getIsRight());
                    answerOptDB.setOptionText(answerOpt.getOptionText());
                    answerOptionDAO.updateByPrimaryKeySelective(answerOptDB);
                }*/
            }
            apiResponse = ApiResponse.success(questionDto);
        } else {
            Date date = new Date();
            q.setCreateTime(date);
            if(admin.getId()!=null)q.setCreator(admin.getId());
            questionDAO.insert(q);
            for (AnswerOption answerOpt : answerOptionList) {
                answerOpt.setQuestionId(q.getId());
                answerOptionDAO.insert(answerOpt);
            }
            questionDto.setId(q.getId());
            apiResponse = ApiResponse.success(questionDto);
        }
        return apiResponse;
    }

    public ApiResponse deleteQuestion(Integer id) {
        ApiResponse apiResponse = null;
        try {
            answerOptionDAO.deleteByQuestionId(id);
            questionDAO.deleteByPrimaryKey(id);
            apiResponse = ApiResponse.success("题目删除成功");
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<QuestionVO> getQuestion(Integer id) {
        ApiResponse apiResponse = null;
        try {

            List<AnswerOption> answerOptionList = answerOptionDAO.selectByQuestionId(id);
            Question q = questionDAO.selectByPrimaryKey(id);
            QuestionVO qVo = new QuestionVO.VOBuilder(q).build();
            qVo.setAnswerOptionList(answerOptionList);
            apiResponse = ApiResponse.success(qVo);
        } catch (Exception e) {
            apiResponse = ApiResponse.error();
            e.printStackTrace();
        }
        return apiResponse;
    }

    public ApiResponse<List<QuestionVO>> findByPageParams(QuestionQueryDTO voParams) {
        ApiResponse apiResponse = null;
        Page page = PageHelper.startPage(voParams.getPageNo(), voParams.getPageSize());
        Admin admin=LoginContextUtil.getAdmin();
        if(voParams.getResourceScope()!=null&&voParams.getResourceScope()!=0){
            if(admin!=null)voParams.setCreator(LoginContextUtil.getAdmin().getId());
        }
        List<Question> resultList = questionDAO.selectByParam(voParams);
        List<QuestionVO> voList = new ArrayList<>();
        for (Question q : resultList) {
            QuestionVO qVo = new QuestionVO.VOBuilder(q).build();
            if (qVo.getOperator() != null) {
                admin = adminDAO.selectByPrimaryKey(q.getOperator());
                if (admin != null) {
                    qVo.setModifyName(admin.getName());
                } else {
                    qVo.setModifyName("");
                }
            }
            voList.add(qVo);
        }
        PageInfo pageInfo = new PageInfo(page);
        pageInfo.setList(resultList);
        apiResponse = ApiResponse.success(voList, (int) pageInfo.getTotal());
        return apiResponse;
    }

    public ApiResponse batchAddQuestion(List<QuestionExcelVO> list) {
        if (list != null && list.size() > 0) {
            Question question;
            AnswerOption aOpt;
            String[] numArray;
            Admin admin=LoginContextUtil.getAdmin();
            for (QuestionExcelVO questionExcelVO : list) {
                question=new Question();
                question.setUpdateTime(new Date());
                if(admin.getId()!=null)question.setOperator(admin.getId());
                //正确答案编号
                numArray=questionExcelVO.getRightNum().split(",");
                question.setObjectType(questionExcelVO.getObjectType());
                question.setExamType(questionExcelVO.getExamType());
                question.setTitle(questionExcelVO.getTitle());
                question.setKeyword(questionExcelVO.getKeyword());
                question.setAnalyzing(questionExcelVO.getAnalyzing());
                questionDAO.insertSelective(question);
                //分析Excel配置的答案
                if(questionExcelVO.getAopt1()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt1());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"1")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }
                if(questionExcelVO.getAopt2()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt2());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"2")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }
                if(questionExcelVO.getAopt3()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt3());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"3")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }
                if(questionExcelVO.getAopt4()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt4());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"4")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }
                if(questionExcelVO.getAopt5()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt5());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"5")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }
                if(questionExcelVO.getAopt6()!=null){
                    aOpt=new AnswerOption();
                    aOpt.setOptionText(questionExcelVO.getAopt6());
                    aOpt.setQuestionId(question.getId());
                    if(ArrayUtils.contains(numArray,"6")){
                        aOpt.setIsRight((byte)1);
                    }
                    answerOptionDAO.insertSelective(aOpt);
                }

            }
        }
        return ApiResponse.success();
    }
}
